package cn.ware.house.interfaces.controller.inbountController;

import cn.ware.house.interfaces.entity.inventory.InboundProduct;
import cn.ware.house.interfaces.service.inboundservice.InboundProductService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/inbound-products")
public class InboundProductController {

    private final InboundProductService inboundProductService;

    public InboundProductController(InboundProductService inboundProductService) {
        this.inboundProductService = inboundProductService;
    }

    // 分页查询入库产品信息（关联查询对应的样式信息）
    @GetMapping("/page")
    public ResponseEntity<List<InboundProduct>> getInboundProductsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        List<InboundProduct> inboundProducts = inboundProductService.getInboundProductsByPage(pageNum, pageSize);
        return new ResponseEntity<>(inboundProducts, HttpStatus.OK);
    }

    // 根据入库产品编号查询产品信息（关联查询对应的样式信息）
    @GetMapping("/{id}")
    public ResponseEntity<InboundProduct> getInboundProductById(@PathVariable Integer id) {
        InboundProduct inboundProduct = inboundProductService.getInboundProductById(id);
        if (inboundProduct!= null) {
            return new ResponseEntity<>(inboundProduct, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 插入入库产品信息
    @PostMapping
    public ResponseEntity<Boolean> addInboundProduct(@RequestBody InboundProduct inboundProduct) {
        List<InboundProduct> inboundProducts =new ArrayList<>();
        inboundProducts.add(inboundProduct);
        boolean result = inboundProductService.addInboundProduct(inboundProducts);
        if (result) {
            return new ResponseEntity<>(true, HttpStatus.CREATED);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 根据入库产品编号更新产品信息
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> updateInboundProductById(
            @PathVariable Integer id,
            @RequestBody InboundProduct inboundProduct) {
        inboundProduct.setId(id);
        boolean result = inboundProductService.updateInboundProductById(inboundProduct);
        if (result) {
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 根据入库产品编号删除产品信息
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteInboundProductById(@PathVariable Integer id) {
        boolean result = inboundProductService.deleteInboundProductById(id);
        if (result) {
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}